package com.example.note.utils;

import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.note.R;
import com.example.note.adapter.ListAdapter;
import com.example.note.bean.Notepad;
import com.example.note.databinding.ActivityMainBinding;
import com.example.note.db.NoteDbHelper;
import com.example.note.ui.*;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;
    private NoteDbHelper db;

    private ListAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        db = new NoteDbHelper(this);
        binding.addIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new android.content.Intent(MainActivity.this, EditAddActivity.class));
            }
        });
        adapter = new ListAdapter();
        binding.ListRv.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        binding.ListRv.setAdapter(adapter);
        binding.ListRv.setLayoutManager(new LinearLayoutManager(this));
        updatepage();
    }

    public void updatepage() {
        List<Notepad> list = db.FindNotes();
        adapter.setlist(list);
    }

    @Override
    protected void onResume() {
        super.onResume();
        updatepage();
    }
}